﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_gesmedicamentos : Form
    {
        System.Data.SqlClient.SqlConnection con;

        public Utilizador util2;
        public Medicamento med1;

        public frm_gesmedicamentos(Utilizador util1)
        {
            this.util2 = util1;
            InitializeComponent();

            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void pesquisa_medicamentos()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT id_medicamento, nome, dosagem, tipo FROM Medicamentos WHERE nome LIKE '%" + txtgm_nome.Text + "%'";
            SqlDataReader readatrib = command1.ExecuteReader();

            this.dataGridView1.Rows.Clear();

            while (readatrib.Read())
            {
                int i = this.dataGridView1.Rows.Add();

                this.dataGridView1[0, i].Value = readatrib["id_medicamento"].ToString();
                this.dataGridView1[1, i].Value = readatrib["nome"].ToString();
                this.dataGridView1[2, i].Value = readatrib["dosagem"].ToString();
                this.dataGridView1[3, i].Value = readatrib["tipo"].ToString();
            }
            con.Close();
        }

        private void pesquisa_todos()
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT id_medicamento, nome, dosagem, tipo FROM Medicamentos";
            SqlDataReader readatrib = command1.ExecuteReader();

            this.dataGridView1.Rows.Clear();

            while (readatrib.Read())
            {
                int i = this.dataGridView1.Rows.Add();

                this.dataGridView1[0, i].Value = readatrib["id_medicamento"].ToString();
                this.dataGridView1[1, i].Value = readatrib["nome"].ToString();
                this.dataGridView1[2, i].Value = readatrib["dosagem"].ToString();
                this.dataGridView1[3, i].Value = readatrib["tipo"].ToString();
            }
            con.Close();
        }

        private void cmdgm_procura_Click(object sender, EventArgs e)
        {
            try
            {
                pesquisa_medicamentos();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdgm_todos_Click(object sender, EventArgs e)
        {
            try
            {
                pesquisa_todos();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.ToString());
            }
        }

        private void cmdgm_voltar_Click(object sender, EventArgs e)
        {
            frm_gesclinica frm1 = new frm_gesclinica(util2);
            this.Close();
            frm1.ShowDialog();
        }

        private void cmdgm_in_Click(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            SqlCommand command1 = con.CreateCommand();
            command1.CommandText = "SELECT MAX(id_medicamento) FROM Medicamentos";
            SqlCommand command2 = con.CreateCommand();
            command2.CommandText = "SELECT Count(*) FROM Medicamentos";
            int num1 = (Int32)command2.ExecuteScalar();
            int num = 0;
            if (num1 == 0)
                num = 1;
            else
            {
                num = (Int32)command1.ExecuteScalar();
                num++;
            }
            con.Close();
            
            Medicamento m1 = new Medicamento(num, "insira o nome", "insira a dosagem", "insira o tipo");

            frm_medicamentos frm1 = new frm_medicamentos(m1, "novo");
            frm1.ShowDialog();
        }

        private void cmdgm_as_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount != 0)
            {
                con = new System.Data.SqlClient.SqlConnection();
                con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
                con.Open();

                int i = this.dataGridView1.CurrentRow.Index;
                string temp_idm = this.dataGridView1.Rows[i].Cells["id_medicamento"].Value.ToString();
                SqlCommand command1 = con.CreateCommand();
                command1.CommandText = "SELECT id_medicamento, nome, dosagem, tipo FROM Medicamentos WHERE id_medicamento = '" + temp_idm + "'";
                SqlDataReader readatrib = command1.ExecuteReader();
                while (readatrib.Read())
                {
                    med1 = new Medicamento(Convert.ToInt32(readatrib["id_medicamento"].ToString()), readatrib["nome"].ToString(), readatrib["dosagem"].ToString(), readatrib["tipo"].ToString());
                }
                
                frm_medicamentos frm2 = new frm_medicamentos(med1, "editar");
                frm2.ShowDialog();
            }
            else
                MessageBox.Show("Tem de selecionar uma linha!", "Por favor selecione", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }
}
